var AnimType = cc.Enum({
    NONE: 0,
    SCALE: 1,
});

var openAnim_scale = function(node, endCbk) {
    var animationTime = 0.5;
    node.x = 10000;
    node.runAction(cc.sequence(
        cc.delayTime(0),
        cc.callFunc(() => {
            node.setScale(0.6);
        }),
        cc.delayTime(0),
        cc.callFunc(() => {
            node.x = 0;
        }),
        cc.scaleTo(animationTime, 1).easing(cc.easeExponentialOut())
    ));
    node.runAction(cc.sequence(
        cc.delayTime(animationTime * 0.85),
        cc.callFunc(endCbk)
    ));
    return animationTime;
}
var closeAnim_scale = function(node, endCbk) {
    node.runAction(cc.sequence(
        cc.scaleTo(0.3, 0.6).easing(cc.easeQuarticActionIn()),
        cc.callFunc(endCbk)
    ));
}
// onPopMessage => onPopMsg
cc.Class({
    extends: cc.Component,

    properties: {
        animType: {
            get: function() {
                return this._animType;
            },
            set: function(value) {
                this._animType = value;
            },
            type: AnimType,
        },
        _animType: AnimType.SCALE,
        isTouchMask: true,
        btnClose: [cc.Button],

        _msgIsReady: false,
        _animationIsReady: false,
        _msgData: null,
        _msgErrData: null,
        _msgErrCode: null,
    },
    editor: {
        menu: 'Common/PopupProxy'
    },

    onLoad: function () {
        // 添加关闭事件
        for (var i = 0; i < this.btnClose.length; i++) {
            this.btnClose[i].node.on("click", this.closePop, this);
        }
    },
    start: function() {
        this.openRequestMsg();
        this.openAnimation();
    },

    // 开启消息请求
    openRequestMsg: function() {
        var script = this.getScript();
        if (script && script._popMsgUrl) {
            LinkHttp.NewLink(script._popMsgUrl, script._popMsgParams)
            .request(this.msgReady, this.msgErrReady).setTarget(this);
        }
        else if (script && script.popRequestMsg) {
            script.popRequestMsg(this.msgReady, this.msgErrReady).setTarget(this);
        }
        else {
            this.msgReady();
        }
    },
    msgReady: function(msg) {
        this._msgIsReady = true;
        this._msgData = msg;
        this.onReady();
    },
    msgErrReady: function(msgErr, code) {
        this._msgIsReady = true;
        this._msgErrData = msgErr;
        this._msgErrCode = code;
        this.onReady();
    },
    // 开启动画
    openAnimation: function() {
        var animationTime = 0.5;
        var script = this.getScript();
        if (script && script.popOpenAnimation) {
            // 自定义动画
            animationTime = script.popOpenAnimation(() => this.animationReady());
        }
        else {
            // 默认动画
            if (this.animType === AnimType.NONE) {
                this.scheduleOnce(() => this.animationReady(), 0);
            }
            else if (this.animType === AnimType.SCALE) {
                animationTime = openAnim_scale(this.node, () => this.animationReady());
            }
        }
        GameToast.showLoading(true, animationTime);
    },
    animationReady: function() {
        this._animationIsReady = true;
        GameToast.showLoading(false);
        this.onReady();
    },
    
    // 开启完毕
    onReady: function() {
        if (!this._animationIsReady || !this._msgIsReady) {
            return;
        }
        var script = this.getScript();
        if (script) {
            if (this._msgErrData) {
                script.onPopMsgError && script.onPopMsgError(this._msgErrData, this._msgErrCode);
            }
            else {
                script.onPopMsg && script.onPopMsg(this._msgData);
            }
        }
    },

    // 关闭
    closePop: function() {
        var script = this.getScript();
        if (script && script.popCloseAnimation) {
            // 自定义关闭动画
            script.popCloseAnimation(() => this.onCloseCbk());
        }
        else {
            // 默认关闭动画
            if (this.animType === AnimType.NONE) {
                this.scheduleOnce(() => this.onCloseCbk(), 0);
            }
            else if (this.animType === AnimType.SCALE) {
                closeAnim_scale(this.node, () => this.onCloseCbk());
            }
        }
        this.node.emit('onClose');
    },
    onCloseCbk: function() {
        this.node.emit('close');
    },
});